package org;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.provider.Settings.Secure;

public class Global {
	
	public static String encryptionKey="";
	//public static String sampleData="{    \"Mastercard\": {        \"Card Number\": \"b\",        \"c\": \"d\",        \"e\": \"f\"    },    \"Visa\": {        \"Card Number\": \"b\",        \"c\": \"d\",        \"e\": \"f\"    }}";
	public static String sampleData="";
	public static String android_id="";
	
	public static void saveData(){
		String eData=encrypt(encryptionKey, sampleData);
		ydSharedPreference.addToPref(Constants.Key_Data, eData);
	//	String dData=de_encrypt(encryptionKey, eData);
		return;
	}
	
	static int eL=5;
	
	public static String encrypt(String key, String data){
		
		String strEncrypted="";
		char c;
		int kl=key.length();
		
		for(int i=0; i < data.length(); i++){
			c=data.charAt(i);
			strEncrypted=strEncrypted.concat(encryptChar(key, c, i));
		}
		
		return strEncrypted;
	}

	private static String encryptChar(String key, char c, int i) {
		String result= ((int)c  * (int)key.charAt((i+key.length())%key.length())) +"";
		
		while(result.length()<eL){
			result="0" + result;
		}
		return result;
	}

	public static void loadData() {
		String eData=ydSharedPreference.getFromPref(org.Constants.Key_Data);
		if(eData.equalsIgnoreCase("nothing")){
			Global.sampleData="";
			return;
		}
		
		Global.sampleData=de_encrypt(encryptionKey, eData);
		return;
	}
	
	public static String de_encrypt(String key, String data){
		
		String strDe_Encrypted="";
		String c;
		int kl=key.length();
		
		for(int i=0; i*eL < data.length(); i++){
			c=data.substring(i*eL, i*eL+eL);
			strDe_Encrypted=strDe_Encrypted + (de_encryptChar(key, c, i));
		}
		
		return strDe_Encrypted;
		
	}

	private static char de_encryptChar(String key, String c, int i) {
		int v=Integer.parseInt(c);
		int result= (v  / (int)key.charAt((i+key.length())%key.length()));
		
		return ((char)result);
	}
	
	public static void getDeviceID(Context c){
		 android_id = Secure.getString(c.getContentResolver(), Secure.ANDROID_ID); 
	}
}
